/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.model.helper;

import com.lingeringdream.luciole.model.Constants;
import com.lingeringdream.luciole.model.entity.Privates;
import com.lingeringdream.luciole.model.entity.User;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.app.Context;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.ArrayList;
import java.util.List;

/**
 * 比较常用的获取实例的方法
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/5/4
 */
public class GetEntityHelper {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x11, GetEntityHelper.class.getSimpleName());

    /**
     * 通过uid获取用户实例
     *
     * @return {@link User}
     */
    public static User getUser(Context context, long uid) {
        User user = null;
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        DataAbilityHelper helper = DataAbilityHelper.create(context);
        predicates.equalTo("uid", uid);
        try {
            ResultSet resultSet = helper.query(Uri.parse(Constants.TABLE_USER.getValue()), null, predicates);
            if (resultSet == null || resultSet.getRowCount() == 0) {
                HiLog.info(TAG, "query: resultSet is null or no result found");
            } else {
                resultSet.goToFirstRow();
                user = ResultSetHelper.resultSet2User(resultSet);
            }
        } catch (Exception e) {
            HiLog.error(TAG, "query: " + e.getMessage());
        }
        helper.release();
        return user;
    }

    /**
     * 获取private实例
     */
    public static List<Privates> getPrivateUser(Context context, String name) {
        List<Privates> privates = new ArrayList<>(1);
        DataAbilityHelper helper = DataAbilityHelper.create(context);
        DataAbilityPredicates predicates = null;
        if (name != null && !"".equals(name)) {
            predicates = new DataAbilityPredicates();
            predicates.equalTo("name", name);
        }
        try {
            ResultSet resultSet = helper.query(Uri.parse(Constants.TABLE_PRIVATE.getValue()), null, predicates);
            if (resultSet == null || resultSet.getRowCount() == 0) {
                HiLog.info(TAG, "query: resultSet is null or no result found");
                return null;
            }
            resultSet.goToFirstRow();
            do {
                privates.add(ResultSetHelper.resultSet2Private(resultSet));
            } while (resultSet.goToNextRow());
        } catch (Exception e) {
            HiLog.error(TAG, "query: " + e.getMessage());
        }
        helper.release();
        return privates;
    }
}
